import { RequestRs } from '../../modules/Request-rs';
import { ReponseRs } from '../../modules/Response-rs';
import { Component, OnInit } from '@angular/core';
import {Product} from '../../modules/product';
import { HttpAssist } from '../../share/assist/http-assist';
import {FormControl} from '@angular/forms';
import {HttpService} from '../../share/service/http.service';
import { UserService } from '../../share/service/user.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {
  private products: Product[];
  private keyword: string;
  private titleFilter: FormControl = new FormControl();
  private imgUrl= 'http://placehold.it/320x150';
  req: RequestRs = new RequestRs();
  res: ReponseRs = new ReponseRs();
  constructor(
    private httpService: HttpService,
    private userService: UserService,
    private router: Router) {
  }
  getProducts() {
    const serviceName = 'getProducts';
    this.req.path = '';
    const message = JSON.stringify(this.req);
    this.httpService.postByJson(serviceName, HttpAssist.GET_PATH, message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS) {
          this.products =  JSON.parse(this.res.message);
        }else {
          alert('fail, detail{' + 'code:' + this.res.status + 'message:'+ this.res.message+'}');
        }
      },
      error => {
          alert('fail,http error in UI, detail' + error);
      }
    );
  }
  ngOnInit(){
    console.info('init products');
    this.getProducts();
  }

  

}
